package net.simflow.swf.analyzer.gui.ginfo.generator;

import java.util.ArrayList;
import java.util.LinkedHashMap;

import net.simflow.swf.analyzer.data.TupleStrInt;
import net.simflow.swf.analyzer.graph.stats.data.GraphStats;
import net.simflow.swf.analyzer.network.data.Flow;
import net.simflow.swf.analyzer.network.stats.data.NetworkStats;
import net.simflow.swf.analyzer.stringtrans.GraphStatsStringGenerator;
import net.simflow.swf.analyzer.stringtrans.NetworkStatsStringGenerator;

public class IOGenerator extends AbstractGenerator {
	protected final static LinkedHashMap<String, Boolean> DEFAULT_CONFIG = new LinkedHashMap<String, Boolean>();
	static {
		DEFAULT_CONFIG.put(GraphStats.GRAPH_NAME, true);
		DEFAULT_CONFIG.put(GraphStats.VERTEX_NUMBER, false);
		DEFAULT_CONFIG.put(GraphStats.VERTICES, false);
		DEFAULT_CONFIG.put(GraphStats.EDGE_NUMBER, false);
		DEFAULT_CONFIG.put(GraphStats.EDGES, false);

		DEFAULT_CONFIG.put(NetworkStats.FLOW, true);
		DEFAULT_CONFIG.put(NetworkStats.NODE, true);
		DEFAULT_CONFIG.put(NetworkStats.SERVICE, true);
		DEFAULT_CONFIG.put(NetworkStats.LINK, true);
	}

	Flow flow;

	private static IOGenerator instance = new IOGenerator();

	private IOGenerator() {
	}

	public static IOGenerator getInstance() {
		return instance;
	}

	public ArrayList<TupleStrInt> generate(GraphStats gstats,
			NetworkStats nstats, LinkedHashMap<String, Boolean> config) {
		return this.gen(gstats, nstats, config, null);
	}

	public ArrayList<TupleStrInt> generate(GraphStats gstats,
			NetworkStats nstats, LinkedHashMap<String, Boolean> config,
			Flow flow) {
		return this.gen(gstats, nstats, config, flow);
	}

	private ArrayList<TupleStrInt> gen(GraphStats gstats, NetworkStats nstats,
			LinkedHashMap<String, Boolean> config, Flow flow) {
		if (gstats == null || nstats == null || config == null) {
			return null;
		}

		this.flow = flow;

		ArrayList<TupleStrInt> info = new ArrayList<TupleStrInt>();
		info.addAll(super.generate(gstats, config));
		info.addAll(super.generate(nstats, config));

		return info;
	}

	@Override
	protected String getTitle(Object stats) {
		if (stats.getClass() == GraphStats.class) {
			return "Graph";
		} else if (stats.getClass() == NetworkStats.class) {
			return "Network";
		}
		return "";
	}

	@Override
	protected ArrayList<TupleStrInt> getInfo(String style, Object stats) {
		if (stats.getClass() == GraphStats.class) {
			return GraphStatsStringGenerator.getInstance().generate(style,
					(GraphStats) stats);
		} else if (stats.getClass() == NetworkStats.class) {
			if (this.flow == null) {
				return NetworkStatsStringGenerator.getInstance().generate(
						style, (NetworkStats) stats);
			} else {
				return NetworkStatsStringGenerator.getInstance().generate(
						style, (NetworkStats) stats, flow);
			}
		}
		return null;
	}

	@Override
	public LinkedHashMap<String, Boolean> getDefaultConfig() {
		return DEFAULT_CONFIG;
	}
}
